package cn.tedu.boyou.website.controller;

import cn.tedu.boyou.acticle.domain.*;
import cn.tedu.boyou.acticle.service.*;
import cn.tedu.boyou.comment.domain.AnswerComment;
import cn.tedu.boyou.comment.service.IAnswerCommentService;
import cn.tedu.boyou.comment.service.IStrategyCommentService;
import cn.tedu.boyou.member.domain.UserInfo;
import cn.tedu.boyou.member.service.IUserInfoService;
import cn.tedu.boyou.search.service.*;
import cn.tedu.boyou.search.template.*;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class DataController {
    //测试mongoDB的保存操作

    @Reference
    private IDestinationSearchService destinationSearchService;
    @Reference
    private IStrategySearchService strategySearchService;
    @Reference
    private IUserInfoSearchService userInfoSearchService;
    @Reference
    private ITravelSearchService travelSearchService;

    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private ITravelService travelService;
    @Reference
    private IStrategyCommentService strategyCommentService;
    @Reference
    private IStrategyTagService strategyTagService;
    @Reference
    private IUserInfoService userInfoService;

    @Reference
    private ISaleSearchService saleSearchService;
    @Reference
    private IRouteSearchService routeSearchService;
    @Reference
    private ITrafficSearchService trafficSearchService;
    @Reference
    private ISaleTagSearchService saleTagSearchService;
    @Reference
    private ISaleTypeSearchService saleTypeSearchService;

    @Reference
    private ISaleService saleService;
    @Reference
    private IRouteService routeService;
    @Reference
    private ITrafficService trafficService;
    @Reference
    private ISaleTagService saleTagService;
    @Reference
    private ISaleTypeService saleTypeService;

    @Reference
    private IAnswerCommentService answerCommentService;

    //添加MongoDB数据库
    @RequestMapping("/mongodb")
    @ResponseBody
    public Object mgdb(){
        AnswerComment answerComment = new AnswerComment();
        answerComment.setAnswerId(1L);
        answerComment.setAnswerTitle("fdsfds");
        answerComment.setCity("广州");
        answerComment.setContent("asdasfdsfdsfdsfasdfasfsadfsd");
        answerComment.setHeadUrl("/images/default.jpg");
        answerComment.setCreateTime(new Date());
        answerComment.setLevel(15);
        answerComment.setRefComment(new AnswerComment());
        answerComment.setUserId(1L);
        answerComment.setUsername("hhhh");
        answerCommentService.save(answerComment);
        return "ok";
    }



    @Reference
    private IAnswerSearchService answerSearchService;

    //创建索引
    @RequestMapping("/createIndex")
    @ResponseBody
    public Object createIndex(){
//        strategySearchService.createIndex();
//        travelSearchService.createIndex();
//        destinationSearchService.createIndex();
//        userInfoSearchService.createIndex();
//        answerSearchService.createIndex();

        saleSearchService.createIndex();
//        routeSearchService.createIndex();
//        trafficSearchService.createIndex();
//        saleTagSearchService.createIndex();
//        saleTypeSearchService.createIndex();

        return "ok";
    }

    @Reference
    private IAnsweringService answeringService;

    //初始化数据
    @RequestMapping("/add")
    @ResponseBody
    public Object add(Model model){

        //旅游路线--------------------------------------------------------------------------------------
//        List<Route> routes = routeService.listAll();
//        for (Route route : routes) {
//            RouteTemplate routeTemplate = new RouteTemplate();
//
//            routeTemplate.setId(route.getId());
//            routeTemplate.setRouteName(route.getName());
//            routeTemplate.setThemeId(route.getThemeId());
//            routeTemplate.setThemeName(route.getThemeName());
//
//            routeSearchService.save(routeTemplate);
//
//        }

        //旅游特色标签
//        List<SaleTag> saleTags = saleTagService.listAll();
//
//        for (SaleTag saleTag : saleTags) {
//            SaleTagTemplate tagTemplate = new SaleTagTemplate();
//
//            tagTemplate.setId(saleTag.getId());
//            tagTemplate.setTag(saleTag.getName());
//
//            saleTagSearchService.save(tagTemplate);
//        }

        //旅游类型
//        List<SaleType> saleTypes = saleTypeService.listAll();
//
//        for (SaleType saleType : saleTypes) {
//            SaleTypeTemplate typeTemplate = new SaleTypeTemplate();
//
//            typeTemplate.setId(saleType.getId());
//            typeTemplate.setName(saleType.getName());
//            typeTemplate.setType(saleType.getType());
//            typeTemplate.setTypeName(saleType.getTypeName());
//
//            saleTypeSearchService.save(typeTemplate);
//        }
//
//        List<Traffic> traffics = trafficService.listAll();
//
//        for (Traffic traffic : traffics) {
//            TrafficTemplate trafficTemplate = new TrafficTemplate();
//
//            trafficTemplate.setId(traffic.getId());
//            trafficTemplate.setTrafficName(traffic.getName());
//
//            trafficSearchService.save(trafficTemplate);
//        }

        //旅游路线--------------------------------------------------------------------------------------
        List<Sale> sales = saleService.listAll();

        for (Sale sale : sales) {
            SaleTemplate st = new SaleTemplate();

            st.setId(sale.getId());
            st.setOriginId(sale.getOriginId());
            st.setOriginName(sale.getOriginName());
            st.setDestId(sale.getDestId());
            st.setDestName(sale.getDestName());
            st.setHeadImgUrl(sale.getHeadImgUrl());
            st.setSoldAmount(sale.getSoldAmount());
            st.setTotalAmount(sale.getTotalAmount());
            st.setTitle(sale.getTitle());
            st.setState_schedule(sale.getStateSchedule());
            st.setEnd_schedule(sale.getEndSchedule());
            st.setShopId(sale.getShopId());
            st.setShopName(sale.getShopName());
            st.setMinPrice(sale.getMinPrice());
            st.setMaxPrice(sale.getMaxPrice());

            TrafficTemplate trafficTemplate = new TrafficTemplate();
            trafficTemplate.setId(sale.getTrafficId());
            trafficTemplate.setTrafficName(sale.getTrafficName());
            st.setTraffic(trafficTemplate);

            List<SaleTagTemplate> saleTagTemplateArrayList = new ArrayList<SaleTagTemplate>();
            List<SaleTag> tags = saleTagService.getTagsBySaleId(sale.getId());
            for (SaleTag tag : tags) {
                SaleTagTemplate saleTagTemplate = new SaleTagTemplate();
                saleTagTemplate.setId(tag.getId());
                saleTagTemplate.setTag(tag.getName());
                saleTagTemplateArrayList.add(saleTagTemplate);
            }
            st.setSaleTags(saleTagTemplateArrayList);

            RouteTemplate hotRouteTemplate = new RouteTemplate();
            Route hotRoute = routeService.getByPrimaryKey(sale.getHotRouteId());
            hotRouteTemplate.setId(hotRoute.getId());
            hotRouteTemplate.setRouteName(hotRoute.getName());
            hotRouteTemplate.setThemeId(hotRoute.getThemeId());
            hotRouteTemplate.setThemeName(hotRoute.getThemeName());
            st.setHotRoute(hotRouteTemplate);

            RouteTemplate funRouteTemplate = new RouteTemplate();
            Route funRoute = routeService.getByPrimaryKey(sale.getFunRouteId());
            funRouteTemplate.setId(funRoute.getId());
            funRouteTemplate.setRouteName(funRoute.getName());
            funRouteTemplate.setThemeId(funRoute.getThemeId());
            funRouteTemplate.setThemeName(funRoute.getThemeName());
            st.setFunRoute(funRouteTemplate);

            //saleType
            SaleTypeTemplate saleTypeTemplate = new SaleTypeTemplate();
            SaleType saleType = saleTypeService.getByPrimaryKey(sale.getSaleTypeId());
            saleTypeTemplate.setId(saleType.getId());
            saleTypeTemplate.setName(sale.getSaleTypeName());
            saleTypeTemplate.setType(saleType.getType());
            saleTypeTemplate.setTypeName(saleType.getTypeName());
            st.setSaleType(saleTypeTemplate);

            st.setPromotion(sale.getPromotion());

            saleSearchService.save(st);
        }



        //问答------------------------------------------------------------------------------------
//        List<Answering> answerings =  answeringService.selectList();
//        for (Answering an : answerings) {
//            AnswerTemplate tmp = new AnswerTemplate();
//            tmp.setId(an.getId());
//            if(an.getContent().length() > 200){
//                tmp.setSummary(an.getContent().substring(0,200));
//            }else {
//                tmp.setSummary(an.getContent());
//            }
//            tmp.setReplynum(an.getReplynum());
//            tmp.setState(an.getState());
//            tmp.setTitle(an.getTitle());
//            tmp.setViewnum(an.getViewnum());
//            UserInfo author = an.getAuthor();
//            tmp.setAuthorId(author.getId());
//            tmp.setAuthorName(author.getNickname());
//            tmp.setAuthorHeadImg(author.getHeadImgUrl());
//            tmp.setAuthorLevel(author.getLevel());
//            Destination dest = an.getDest();
//            tmp.setDestId(dest.getId());
//            tmp.setDestName(dest.getName());
//            tmp.setCreateTime(an.getCreateTime());
//            tmp.setCity(author.getCity());
//            answerSearchService.save(tmp);
//        }
        ///攻略----------------------------------------------------------------------------
      /*  List<StrategyDetail> all = strategyDetailService.listAll();
        for (StrategyDetail detail : all) {
            StrategyTemplate t = new StrategyTemplate();

            t.setId(detail.getId());
            t.setTitle(detail.getTitle());
            t.setSubTitle(detail.getSubTitle());
            t.setDestId(detail.getDest().getId());
            t.setDestName(detail.getDest().getName());
            t.setThemeId(detail.getTheme().getId());
            t.setThemeName(detail.getTheme().getName());
            t.setSummary(detail.getSummary());
            t.setCreateTime(detail.getCreateTime());
            t.setViewnum(detail.getViewnum());
            t.setFavornum(detail.getFavornum());
            t.setReplynum(detail.getReplynum());
            t.setThumbsupnum(detail.getThumbsupnum());

            t.setCoverUrl(detail.getCoverUrl());


            List<String> list = strategyTagService.getByDestId(detail.getId());
            t.setTags(list);


            Destination dest = destinationService.getCountry(detail.getDest().getId());

            t.setCountryId(dest.getId());
            t.setCountryName(dest.getName());
            dest = destinationService.getProvince(detail.getDest().getId());
            if(dest != null){
                t.setProvinceId(dest.getId());
                t.setProvinceName(dest.getName());
            }

            strategySearchService.save(t);
        }

        ///游记----------------------------------------------------------------------------
        List<Travel> ts = travelService.list();

        for (Travel t : ts) {
            TravelTemplate tt = new TravelTemplate();
            tt.setId(t.getId());
            tt.setAuthorId(t.getAuthor().getId());
            tt.setAuthorName(t.getAuthor().getNickname());
            tt.setCoverUrl(t.getCoverUrl());
            tt.setCreateTime(t.getCreateTime());
            tt.setDestId(t.getDest().getId());
            tt.setDestName(t.getDest().getName());
            tt.setReplynum(t.getReplynum());
            tt.setSummary(t.getSummary());
            tt.setTitle(t.getTitle());
            tt.setViewnum(t.getViewnum());
            travelSearchService.save(tt);
        }

        //用户----------------------------------------------------------------------------
        List<UserInfo> us = userInfoService.list();
        for (UserInfo u : us) {
            UserInfoTemplate tt = new UserInfoTemplate();
            tt.setId(u.getId());
            tt.setDestName(u.getCity());
            tt.setFansnum(11);
            tt.setHeadUrl(u.getHeadImgUrl());
            tt.setInfo(u.getInfo());
            tt.setLevel(u.getLevel());
            tt.setNickname(u.getNickname());
            tt.setReplynum(10);
            tt.setTravelnum(2);
            userInfoSearchService.save(tt);
        }
*/
        //目的地----------------------------------------------------------------------------
       /* List<Destination> ds = destinationService.getDestByDeep(3);

        for (Destination d : ds) {

            DestinationTemplate tt = new DestinationTemplate();

            tt.setId(d.getId());
            tt.setCoverUrl(d.getCoverUrl());
            tt.setName(d.getName());
            tt.setInfo(d.getInfo());
            destinationSearchService.save(tt);
        }*/




        return "ok";
    }
}
